var formType;
mini.parse();

var form = new mini.Form("captainForm");

var remainUserList;


function beforenodeselect(e) {
    //禁止选中父节点
    if (e.isLeaf == false) e.cancel = true;
}

function onCloseClick(e) {
    var obj = e.sender;
    obj.setText("");
    obj.setValue("");
}

$(function () {
    initRemainUserList();
    initRemainUserData(remainUserList,'');
})


function initRemainUserList(){
    $.ajax({
        url: GatewayUrl() + '/captain/initRemainUserList',
        type: 'get',
        async: false,
        headers: {
            "Authorization": localStorage.getItem("token")
        },
        success: function (dt) {
            if (dt.code === 200) {
                remainUserList = dt.data;
            }
        }
    })
}

function initRemainUserData(data,captainId) {
    var obj = mini.get("Uuid");
    obj.setData(data);
    if(captainId !== ''){
        obj.setValue(captainId)
    }
}

function SaveData() {
    var o = form.getData();
    var data = {"captainId": JSON.parse(localStorage.getItem("user")).uuid,"uuid": o.uuid};
    form.validate();
    if (form.isValid() == false) return;
    if(o.uuid !== ''){
        $.ajax({
            url: GatewayUrl() + "/captain/add",
            type: 'post',
            contentType: "application/json",
            data: JSON.stringify(data),
            headers: {
                "Authorization": localStorage.getItem("token")
            },
            success: function (dt) {
                if (dt.code === 200) {
                    CloseWindow("save");
                }

            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
                CloseWindow();
            }
        });
        
    }
}




////////////////////
//标准方法接口定义
function SetData(data) {
    
    formType = 'add';
    
}




function GetData() {
    var o = form.getData();
    return o;
}
function CloseWindow(action) {
    if (action == "close" && form.isChanged()) {
        if (confirm("数据被修改了，是否先保存？")) {
            return false;
        }
    }
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
function onOk(e) {
    if (formType == 'add') {
        mini.confirm("确定添加？", "确定？",
            function (action) {
                if (action == "ok") {
                    SaveData();
                }
            }
        );
    } else {
        mini.confirm("确定修改？", "确定？",
            function (action) {
                if (action == "ok") {
                    UpdateData();
                }
            }
        );
    }

}
function onCancel(e) {
    CloseWindow("cancel");
}